package com.uzai.console.service.merchant.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.*;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.CommonUtil;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.IpUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.constant.Const;
import com.uzai.console.dto.login.RegisterDto;
import com.uzai.console.dto.profit.ladderratio.LadderRatioQuery;
import com.uzai.console.dto.profit.ladderratiogroup.LadderRatioGroupQuery;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleQuery;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleQuery;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupQuery;
import com.uzai.console.dto.profit.prirewardrule.PriRewardRuleQuery;
import com.uzai.console.dto.profit.regrewardrule.RegRewardRuleQuery;
import com.uzai.console.dto.user.usertype.UserTypeQuery;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupQuery;
import com.uzai.console.entity.*;
import com.uzai.console.enums.SmsType;
import com.uzai.console.mapper.*;
import com.uzai.console.service.merchant.RegisterService;
import com.uzai.console.service.rebotcmd.RobotCmdService;
import com.uzai.console.vo.cms.base.CmsBaseInfoVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RegisterServiceImpl implements RegisterService{

    Logger logger = LoggerFactory.getLogger(RegisterServiceImpl.class);

    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RegRewardRuleMapper regRewardRuleMapper;
    @Autowired
    private InviteRewardRuleMapper inviteRewardRuleMapper;
    @Autowired
    private PriRewardRuleMapper priRewardRuleMapper;
    @Autowired
    private LadderRatioGroupMapper ladderRatioGroupMapper;
    @Autowired
    private LadderRatioMapper ladderRatioMapper;
    @Autowired
    private NuserRewardRuleGroupMapper nuserRewardRuleGroupMapper;
    @Autowired
    private NuserRewardRuleMapper nuserRewardRuleMapper;
    @Autowired
    private UserTypeMapper userTypeMapper;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;
    @Autowired
    private NormalRewardRuleMapper normalRewardRuleMapper;
    @Autowired
    private ConfigPlanMapper configPlanMapper;
    @Autowired
    private SysPluginMapper sysPluginMapper;
    @Autowired
    private RobotCmdService robotCmdService;
    @Autowired
    private RobotCmdMapper robotCmdMapper;

    /**
     * 注册
     * @param registerDto
     */
    @Override
    @Transactional
    public void register(RegisterDto registerDto) {



        //判断输入的手机号是否正确
        if (StringUtils.isBlank(registerDto.getMobile()) || registerDto.getMobile().length() != 11){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MOBILE_ERROR);
        }

        //判断手机号是否使用
        Merchant merchantByPhone = merchantMapper.selectByMobile(Tools.getStr(registerDto.getMobile()));
        if (merchantByPhone != null){
            throw new BusinessException(ResponseCode.ERROR,ResponseMsg.MOBILE_USE);
        }

        //获取存在redis中的验证码
        String codeByRedis = (String) redisTemplate.opsForValue().get(String.format(SmsType.REGISTER.getDesc(), registerDto.getMobile()));
        //判断验证码是否过期
        if (StringUtils.isBlank(codeByRedis)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_MISS);
        }

        //验证码是否正确
        if (!codeByRedis.equals(registerDto.getCode())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_ERR);
        }

        //获取当前时间
        Integer now = DateUtil.getNowTime();

        //获取请求客户端的IP
        String ip = Tools.getStr(IpUtil.getRequestIp());

        Merchant merchant = new Merchant();
        long merId = IdWorker.getId();
        merchant.setId(merId);
        merchant.setMobile(registerDto.getMobile());
        merchant.setCloudPwd(registerDto.getCloudPwd());
        //设备密码为随机6位数字
        String devicePwd = RandomStringUtils.randomNumeric(6);
        merchant.setDevicePwd(devicePwd);
        merchant.setBalance(0.0);
        merchant.setRegTime(now);
        merchant.setToken(CommonUtil.getUUID());
        merchant.setRegIp(ip);
        merchant.setOtherEsIndex(Const.Merchant.REGIST_OTHER_ES_INDEX);
        merchant.setCreateTime(now);
        merchant.setUpdateTime(now);
        merchantMapper.insert(merchant);

        //维护Redis当中的运营商信息
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.MERCHANT.getKey(), merchant.getId()) , Tools.getStrEmpty(JSONObject.toJSONString(merchant)));


        //默认增加各种配置,
        //先获取默认运营商id
        Long defaultMerId = uzaiConsoleSysConfig.getDefaultMerId();

        //配置CMS基础设置信息
        MerBaseConfig merBaseConfig_cmsBase = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.CMS_BASE_CONFIG.getValue());
        if(merBaseConfig_cmsBase != null ){
            CmsBaseInfoVo cmsBaseInfoVo_defult =  JSONObject.parseObject(merBaseConfig_cmsBase.getVal(), CmsBaseInfoVo.class);
            if(cmsBaseInfoVo_defult != null){
                CmsBaseInfoVo cmsBaseInfoVo = new CmsBaseInfoVo();
                cmsBaseInfoVo.setMallName(cmsBaseInfoVo_defult.getMallName());
                cmsBaseInfoVo.setMallDomain(cmsBaseInfoVo_defult.getMallDomain());
                cmsBaseInfoVo.setThemeColor(cmsBaseInfoVo_defult.getThemeColor());
                cmsBaseInfoVo.setEstimatedIncomeSwitch(cmsBaseInfoVo_defult.getEstimatedIncomeSwitch());
                cmsBaseInfoVo.setUserTypeSwitch(cmsBaseInfoVo_defult.getUserTypeSwitch());
                cmsBaseInfoVo.setCmsSupportTbSwitch(cmsBaseInfoVo_defult.getCmsSupportTbSwitch());
                cmsBaseInfoVo.setCmsSupportJdSwitch(cmsBaseInfoVo_defult.getCmsSupportJdSwitch());
                cmsBaseInfoVo.setCmsSupportPddSwitch(cmsBaseInfoVo_defult.getCmsSupportPddSwitch());
                cmsBaseInfoVo.setCmsSupportVphSwitch(cmsBaseInfoVo_defult.getCmsSupportVphSwitch());
                cmsBaseInfoVo.setXcxSupportTbSwitch(cmsBaseInfoVo_defult.getXcxSupportTbSwitch());
                cmsBaseInfoVo.setXcxSupportJdSwitch(cmsBaseInfoVo_defult.getXcxSupportJdSwitch());
                cmsBaseInfoVo.setXcxSupportPddSwitch(cmsBaseInfoVo_defult.getXcxSupportPddSwitch());
                cmsBaseInfoVo.setXcxSupportVphSwitch(cmsBaseInfoVo_defult.getXcxSupportVphSwitch());

                merBaseConfig_cmsBase.setId(IdWorker.getId());
                merBaseConfig_cmsBase.setMerId(merchant.getId());
                merBaseConfig_cmsBase.setCreateTime(now);
                merBaseConfig_cmsBase.setUpdateTime(now);
                merBaseConfig_cmsBase.setVal(Tools.getStrEmpty(JSONObject.toJSONString(cmsBaseInfoVo)));
                merBaseConfigMapper.insert(merBaseConfig_cmsBase);
                //保存到Redis当中
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.CMS_BASE_CONFIG.getKey(), merchant.getId()), Tools.getStrEmpty(JSONObject.toJSONString(cmsBaseInfoVo)));

            }
        }

        //配置CMS首页配置信息
        MerBaseConfig merBaseConfig_homepage = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.CMS_HOMEPAGE_CONFIG.getValue());
        if(merBaseConfig_homepage != null ){
            merBaseConfig_homepage.setId(IdWorker.getId());
            merBaseConfig_homepage.setMerId(merchant.getId());
            merBaseConfig_homepage.setCreateTime(now);
            merBaseConfig_homepage.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig_homepage);
            //保存到Redis当中
            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.CMS_HOMEPAGE_CONFIG.getKey(), merchant.getId()), merBaseConfig_homepage.getVal());
        }

        //配置CMS个人中心
        MerBaseConfig merBaseConfig_personal_center = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getValue());
        if(merBaseConfig_personal_center != null ){
            merBaseConfig_personal_center.setId(IdWorker.getId());
            merBaseConfig_personal_center.setMerId(merchant.getId());
            merBaseConfig_personal_center.setCreateTime(now);
            merBaseConfig_personal_center.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig_personal_center);
            //更新redis
            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getKey(), merchant.getId()), merBaseConfig_personal_center.getVal());
        }

        //配置悠聊基础配置信息
        MerBaseConfig merBaseConfig_uzaichat_base = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.UZAICHAT_BASE_CONFIG.getValue());
        if(merBaseConfig_uzaichat_base != null ){
            merBaseConfig_uzaichat_base.setId(IdWorker.getId());
            merBaseConfig_uzaichat_base.setMerId(merchant.getId());
            merBaseConfig_uzaichat_base.setCreateTime(now);
            merBaseConfig_uzaichat_base.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig_uzaichat_base);
            //保存到Redis当中
            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.UZAICHAT_BASE_CONFIG.getKey(), merchant.getId()), merBaseConfig_uzaichat_base.getVal());
        }

        //开启私域模块
        MerBaseConfig merBaseConfig_device = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.DEVICE_SWITCH.getValue());
        if(merBaseConfig_device != null){
            merBaseConfig_device.setId(IdWorker.getId());
            merBaseConfig_device.setMerId(merchant.getId());
            merBaseConfig_device.setCreateTime(now);
            merBaseConfig_device.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig_device);
            //保存redis
            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_SWITCH.getKey(), merchant.getId()), merBaseConfig_device.getVal());
        }

        //开启功能模块
        MerBaseConfig merBaseConfig_product = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.PRODUCT_SWITCH.getValue());
        if(merBaseConfig_product != null){
            merBaseConfig_product.setId(IdWorker.getId());
            merBaseConfig_product.setMerId(merchant.getId());
            merBaseConfig_product.setCreateTime(now);
            merBaseConfig_product.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig_product);
            //保存redis
            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PRODUCT_SWITCH.getKey(), merchant.getId()), merBaseConfig_product.getVal());
        }

        //分佣管理
        //注册奖励方案-默认只添加第一条
        RegRewardRuleQuery regRewardRuleQuery = new RegRewardRuleQuery();
        regRewardRuleQuery.setMerId(defaultMerId);
        List<RegRewardRule> regRewardRuleList = regRewardRuleMapper.findByList(regRewardRuleQuery);
        if(regRewardRuleList != null && regRewardRuleList.size() > 0){
            RegRewardRule regRewardRule = regRewardRuleList.get(0);
            //先添加邀请奖励信息
            //解析JSON.得到邀请奖励id
            JSONObject dataObject = JSONObject.parseObject(regRewardRule.getData());
            if(dataObject != null){
                //邀请奖励id
                Long inviteRewardId = dataObject.getLong("invite_reward_id");
                if(inviteRewardId != null){
                    InviteRewardRule inviteRewardRule = inviteRewardRuleMapper.selectById(inviteRewardId, regRewardRule.getMerId());
                    if(inviteRewardRule != null){
                        //添加邀请奖励规则
                        inviteRewardRule.setId(IdWorker.getId());
                        inviteRewardRule.setMerId(merId);
                        inviteRewardRule.setCreateTime(now);
                        inviteRewardRule.setUpdateTime(now);
                        inviteRewardRuleMapper.insert(inviteRewardRule);
                        //修改注册奖励JSON的邀请奖励id
                        dataObject.put("invite_reward_id", inviteRewardRule.getId());
                        regRewardRule.setData(dataObject.toJSONString());
                    }
                }
            }
            regRewardRule.setId(IdWorker.getId());
            regRewardRule.setMerId(merId);
            regRewardRule.setCreateTime(now);
            regRewardRule.setUpdateTime(now);
            regRewardRuleMapper.insert(regRewardRule);
        }

        //定向规则
        PriRewardRuleQuery priRewardRuleQuery = new PriRewardRuleQuery();
        priRewardRuleQuery.setMerId(defaultMerId);
        List<PriRewardRule> priRewardRuleList = priRewardRuleMapper.findByList(priRewardRuleQuery);
        if(priRewardRuleList != null && priRewardRuleList.size() > 0){
            for (PriRewardRule priRewardRule : priRewardRuleList){
                priRewardRule.setId(IdWorker.getId());
                priRewardRule.setMerId(merId);
                priRewardRule.setCreateTime(now);
                priRewardRule.setUpdateTime(now);
                priRewardRuleMapper.insert(priRewardRule);
            }
        }

        //阶梯规则
        //阶梯规则组id
        Long ladderRatioGroupId = IdWorker.getId();
        LadderRatioGroupQuery ladderRatioGroupQuery = new LadderRatioGroupQuery();
        ladderRatioGroupQuery.setMerId(defaultMerId);
        List<LadderRatioGroup> ladderRatioGroupList = ladderRatioGroupMapper.findByList(ladderRatioGroupQuery);
        if(ladderRatioGroupList != null && ladderRatioGroupList.size() > 0){
            LadderRatioGroup ladderRatioGroup = ladderRatioGroupList.get(0); //取第一个
            Long id_old = ladderRatioGroup.getId(); //旧分组id
            ladderRatioGroup.setId(ladderRatioGroupId);
            ladderRatioGroup.setMerId(merId);
            ladderRatioGroup.setCreateTime(now);
            ladderRatioGroup.setUpdateTime(now);
            ladderRatioGroupMapper.insert(ladderRatioGroup);

            //查询该阶梯分组下的所有的阶梯规则
            LadderRatioQuery ladderRatioQuery = new LadderRatioQuery();
            ladderRatioQuery.setMerId(defaultMerId);
            ladderRatioQuery.setGroupId(id_old);
            List<LadderRatio> ladderRatioList = ladderRatioMapper.findByList(ladderRatioQuery);
            if(ladderRatioList != null && ladderRatioList.size() > 0) {
                for (LadderRatio ladderRatio : ladderRatioList){
                    //添加阶梯规则
                    ladderRatio.setId(IdWorker.getId());
                    ladderRatio.setMerId(merId);
                    ladderRatio.setGroupId(ladderRatioGroupId);
                    ladderRatio.setCreateTime(now);
                    ladderRatio.setUpdateTime(now);
                    ladderRatioMapper.insert(ladderRatio);
                }
            }
        }

        //杀熟规则
        //杀熟规则组id
        Long nuserRewardRuleGroupId = IdWorker.getId();
        NuserRewardRuleGroupQuery nuserRewardRuleGroupQuery = new NuserRewardRuleGroupQuery();
        nuserRewardRuleGroupQuery.setMerId(defaultMerId);
        List<NuserRewardRuleGroup> nuserRewardRuleGroupList = nuserRewardRuleGroupMapper.findByList(nuserRewardRuleGroupQuery);
        if(nuserRewardRuleGroupList != null && nuserRewardRuleGroupList.size() > 0){
            NuserRewardRuleGroup nuserRewardRuleGroup = nuserRewardRuleGroupList.get(0); ////取第一个
            Long id_old = nuserRewardRuleGroup.getId();
            nuserRewardRuleGroup.setId(nuserRewardRuleGroupId);
            nuserRewardRuleGroup.setMerId(merId);
            nuserRewardRuleGroup.setCreateTime(now);
            nuserRewardRuleGroup.setUpdateTime(now);
            nuserRewardRuleGroupMapper.insert(nuserRewardRuleGroup);

            NuserRewardRuleQuery nuserRewardRuleQuery = new NuserRewardRuleQuery();
            nuserRewardRuleQuery.setMerId(defaultMerId);
            nuserRewardRuleQuery.setGroupId(id_old);
            List<NuserRewardRule> nuserRewardRuleList = nuserRewardRuleMapper.findByList(nuserRewardRuleQuery);
            if(nuserRewardRuleList != null && nuserRewardRuleList.size() > 0){
                for (NuserRewardRule nuserRewardRule : nuserRewardRuleList){
                    //添加杀熟规则
                    nuserRewardRule.setId(IdWorker.getId());
                    nuserRewardRule.setMerId(merId);
                    nuserRewardRule.setGroupId(nuserRewardRuleGroupId);
                    nuserRewardRule.setLadderRatioGroupId(ladderRatioGroupId);
                    nuserRewardRule.setCreateTime(now);
                    nuserRewardRule.setUpdateTime(now);
                    nuserRewardRuleMapper.insert(nuserRewardRule);
                }
            }
        }

        //会员类型组
        //会员类型组ID
        Long userTypeGroupId = IdWorker.getId();
        UserTypeGroupQuery userTypeGroupQuery = new UserTypeGroupQuery();
        userTypeGroupQuery.setMerId(defaultMerId);
        List<UserTypeGroup> userTypeGroupList = userTypeGroupMapper.findByList(userTypeGroupQuery);
        if(userTypeGroupList != null && userTypeGroupList.size() > 0){
            UserTypeGroup userTypeGroup = userTypeGroupList.get(0);
            userTypeGroup.setId(userTypeGroupId);
            userTypeGroup.setMerId(merId);
            userTypeGroup.setCreateTime(now);
            userTypeGroup.setUpdateTime(now);
            userTypeGroupMapper.insert(userTypeGroup);
        }

        //分佣规则
        Long commission_id = IdWorker.getId();
        NormalRewardRuleQuery normalRewardRuleQuery = new NormalRewardRuleQuery();
        normalRewardRuleQuery.setMerId(defaultMerId);
        List<NormalRewardRule> normalRewardRuleList = normalRewardRuleMapper.findByList(normalRewardRuleQuery);
        if(normalRewardRuleList != null && normalRewardRuleList.size() > 0){
            NormalRewardRule normalRewardRule = normalRewardRuleList.get(0);
            normalRewardRule.setId(commission_id);
            normalRewardRule.setMerId(merId);
            normalRewardRule.setNuserRuleGroupId(nuserRewardRuleGroupId);
            normalRewardRule.setUserTypeGroupId(userTypeGroupId);
            normalRewardRule.setCreateTime(now);
            normalRewardRule.setUpdateTime(now);
            //奖励明细
            String rewardDetail = normalRewardRule.getRewardDetail();
            if(StringUtils.isNotBlank(rewardDetail)){
                JSONArray rewardArr =  JSONObject.parseArray(rewardDetail);
                if(rewardArr != null && rewardArr.size() > 0) {
                    for (int i = 0; i < rewardArr.size(); i++) {
                        JSONObject jsonObject = (JSONObject) rewardArr.get(i);
                        //阶梯规则ID
                        jsonObject.put("ladderRatioGroupId", String.valueOf(ladderRatioGroupId));
                        //会员类型ID
                        Long userTypeId = jsonObject.getLong("userType");
                        UserType userType = userTypeMapper.selectById(userTypeId, defaultMerId);
                        if(userType != null){
                            userType.setId(IdWorker.getId());
                            userType.setMerId(merId);
                            userType.setGroupId(userTypeGroupId);
                            userType.setCreateTime(now);
                            userType.setUpdateTime(now);
                            userTypeMapper.insert(userType);
                            //修改会员id
                            jsonObject.put("userType", String.valueOf(userType.getId()));
                        }
                    }
                    normalRewardRule.setRewardDetail(rewardArr.toJSONString());
                }
            }
            normalRewardRuleMapper.insert(normalRewardRule);
        }

        //个人微信配置方案
        //查找默认的配置方案
        if(true){
            ConfigPlan configPlan_wechat = configPlanMapper.selectByIsDefault(defaultMerId, DeviceTypeEnum.DT_PC_WECHAT.getKey());
            if(configPlan_wechat != null){
                //复制插件件信息
                List<SysPlugin> sysPluginsList = new ArrayList<SysPlugin>();
                Long configPlanId = IdWorker.getId();

                //淘宝插件ID
                if(configPlan_wechat.getTbId() != null ){
                    //查询淘宝插件
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getTbId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的淘宝插件
                        configPlan_wechat.setTbId(id);
                    }
                }

                //京东插件id
                if(configPlan_wechat.getJdId()!= null ){
                    //查询京东插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getJdId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加京东插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的京东插件id
                        configPlan_wechat.setJdId(id);
                    }
                }

                //拼多多插件id
                if(configPlan_wechat.getPddId()!= null ){
                    //查询拼多多插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getPddId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加拼多多插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的拼多多插件id
                        configPlan_wechat.setPddId(id);
                    }
                }

                //唯品会插件id
                if(configPlan_wechat.getVphId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getVphId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wechat.setVphId(id);
                    }
                }

                //本地生活插件id
                if(configPlan_wechat.getLocalLiveId()!= null ){
                    //查询本地生活插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getLocalLiveId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加本地生活插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的本地生活插件id
                        configPlan_wechat.setLocalLiveId(id);

                        //本地生活指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.LOCAL_LIVE.getValue(), sysPlugin.getData());
                    }
                }

                //抖音插件id
                if(configPlan_wechat.getDyId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getDyId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wechat.setDyId(id);
                    }
                }

                //快手插件id
                if(configPlan_wechat.getKsId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getKsId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wechat.setKsId(id);
                    }
                }

                //更新分佣规则
                //会员类型组
                configPlan_wechat.setUserTypeGroupId(userTypeGroupId);
                //淘宝分佣规则
                configPlan_wechat.setTbCommissionPlanId(commission_id);
                //京东分佣规则
                configPlan_wechat.setJdCommissionPlanId(commission_id);
                //拼多多分佣规则
                configPlan_wechat.setPddCommissionPlanId(commission_id);
                //唯品会分佣规则
                configPlan_wechat.setVphCommissionPlanId(commission_id);
                //抖音分佣规则
                configPlan_wechat.setDyCommissionPlanId(commission_id);
                //本地生活分佣规则
                configPlan_wechat.setLocalLiveCommissionPlanId(commission_id);

                //拉群规则
                if(configPlan_wechat.getPullWxgroupRuleId() != null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getPullWxgroupRuleId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_wechat.setPullWxgroupRuleId(id);
                    }
                }

                //微信好友添加插件id
                if(configPlan_wechat.getWxUserAddId()!= null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getWxUserAddId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_wechat.setWxUserAddId(id);
                    }
                }

                //推广裂变插件id
                if(configPlan_wechat.getUserExpandFissionId()!= null ){
                    //查询推广裂变插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getUserExpandFissionId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加推广裂变插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);

                        //清空提现账房信息-每个运营商账房信息不一样
                        String data = sysPlugin.getData();
                        if(StringUtils.isNotBlank(data)){
                            try {
                                JSONObject jsonObject = JSONObject.parseObject(data);
                                if(jsonObject != null){
                                    jsonObject.put("zf_app_id", "");
                                    jsonObject.put("zf_app_key", "");
                                    jsonObject.put("wxpay_zf_app_id", "");
                                    jsonObject.put("wxpay_zf_app_key", "");
                                }
                                sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                            }catch (Exception e){
                                logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                            }
                        }

                        //修改配置方案的推广裂变插件id
                        configPlan_wechat.setUserExpandFissionId(id);
                    }
                }

                //提现插件id
                if(configPlan_wechat.getUserTakeBalId()!= null ){
                    //查询提现插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getUserTakeBalId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加提现插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中提现插件id
                        configPlan_wechat.setUserTakeBalId(id);

                        //保存提现指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_TAKE_BAL.getValue(), sysPlugin.getData());
                    }
                }

                //好友引导插件id
                if(configPlan_wechat.getUserGuideId()!= null ){
                    //好友引导插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getUserGuideId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加好友引导插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中好友引导插件id
                        configPlan_wechat.setUserGuideId(id);
                    }
                }

                //自动备注插件id
                if(configPlan_wechat.getUserAutoRemarkId()!= null ){
                    //自动备注插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getUserAutoRemarkId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加自动备注插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中自动备注插件id
                        configPlan_wechat.setUserAutoRemarkId(id);
                    }
                }

                //订阅消息插件id
                if(configPlan_wechat.getSubscribeMsgId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getSubscribeMsgId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加订阅消息插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中订阅消息插件id
                        configPlan_wechat.setSubscribeMsgId(id);

                        //保存订阅指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.SUBSCRIBE_MSG.getValue(), sysPlugin.getData());
                    }
                }

                //个人信息查询插件id
                if(configPlan_wechat.getUserInfoQueryId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getUserInfoQueryId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加个人信息查询插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中个人信息查询插件id
                        configPlan_wechat.setUserInfoQueryId(id);

                        //保存个人信息指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_INFO_QUERY.getValue(), sysPlugin.getData());
                    }
                }

                //用户签到插件id
                if(configPlan_wechat.getUserSigninId()!= null ){
                    //用户签到插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getUserSigninId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加用户签到插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中用户签到插件id
                        configPlan_wechat.setUserSigninId(id);

                        //保存用户签到指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_SIGNIN.getValue(), sysPlugin.getData());
                    }
                }

                //社群管理插件id
                if(configPlan_wechat.getCommunityId()!= null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getCommunityId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_wechat.setCommunityId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //其他设置插件id
                if(configPlan_wechat.getOtherId()!= null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getOtherId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wechat.setOtherId(id);
                    }
                }

                //导购插件id
                if(configPlan_wechat.getShoppingGuideId() != null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getShoppingGuideId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_wechat.setShoppingGuideId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //微信公众号设置id
                if(configPlan_wechat.getWxpubSettingId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getWxpubSettingId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wechat.setWxpubSettingId(id);
                    }
                }

                //微信机器人关联公众号插件id
                if(configPlan_wechat.getWechatUnionWxpubId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechat.getWechatUnionWxpubId(), configPlan_wechat.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wechat.setWechatUnionWxpubId(id);
                    }
                }

                //批量插入插件
                if(sysPluginsList != null & sysPluginsList.size() > 0){
                    sysPluginMapper.batchAdd(sysPluginsList);
                    for(SysPlugin sysPlugin : sysPluginsList){
                        //修改插件更新缓存，确保缓存和数据库一致
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                    }
                }

                //查出旧指令列表
                List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_wechat.getMerId(), configPlan_wechat.getId());
                if(robotCmdList != null  && robotCmdList.size() > 0){
                    for (RobotCmd robotCmd : robotCmdList){
                        robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                    }

                    //批量插入指令表数据库
                    robotCmdMapper.batchAdd(robotCmdList);

                    //更新新的指令缓存redis
                    String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                }

                //添加新的配置方案
                configPlan_wechat.setId(configPlanId);
                configPlan_wechat.setMerId(merId);
                configPlan_wechat.setCreateTime(now);
                configPlan_wechat.setUpdateTime(now);
                configPlan_wechat.setIsDefault(1);

                //清除联盟设置
                configPlan_wechat.setTbAccountId(null);
                configPlan_wechat.setJdAccountId(null);
                configPlan_wechat.setPddAccountId(null);
                configPlan_wechat.setVphAccountId(null);
                configPlan_wechat.setTlbPddAccountId(null);
                configPlan_wechat.setTlbMtAccountId(null);
                configPlan_wechat.setElmAccountId(null);
                configPlan_wechat.setTlbElmAccountId(null);
                configPlan_wechat.setDyAccountId(null);

                configPlanMapper.insert(configPlan_wechat);

            }
        }


        //企业微信配置方案
        if(true){
            //查找默认的企业微信配置方案
            ConfigPlan configPlan_wxwork = configPlanMapper.selectByIsDefault(defaultMerId, DeviceTypeEnum.DT_WORK_WECHAT.getKey());
            if(configPlan_wxwork != null){
                //复制插件件信息
                List<SysPlugin> sysPluginsList = new ArrayList<SysPlugin>();
                Long configPlanId = IdWorker.getId();

                //淘宝插件ID
                if(configPlan_wxwork.getTbId() != null ){
                    //查询淘宝插件
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getTbId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的淘宝插件
                        configPlan_wxwork.setTbId(id);
                    }
                }

                //京东插件id
                if(configPlan_wxwork.getJdId()!= null ){
                    //查询京东插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getJdId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加京东插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的京东插件id
                        configPlan_wxwork.setJdId(id);
                    }
                }

                //拼多多插件id
                if(configPlan_wxwork.getPddId()!= null ){
                    //查询拼多多插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getPddId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加拼多多插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的拼多多插件id
                        configPlan_wxwork.setPddId(id);
                    }
                }

                //唯品会插件id
                if(configPlan_wxwork.getVphId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getVphId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wxwork.setVphId(id);
                    }
                }

                //本地生活插件id
                if(configPlan_wxwork.getLocalLiveId()!= null ){
                    //查询本地生活插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getLocalLiveId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加本地生活插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的本地生活插件id
                        configPlan_wxwork.setLocalLiveId(id);

                        //本地生活指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.LOCAL_LIVE.getValue(), sysPlugin.getData());
                    }
                }

                //抖音插件id
                if(configPlan_wxwork.getDyId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getDyId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wxwork.setDyId(id);
                    }
                }

                //快手插件id
                if(configPlan_wxwork.getKsId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getKsId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wxwork.setKsId(id);
                    }
                }

                //更新分佣规则
                //会员类型组
                configPlan_wxwork.setUserTypeGroupId(userTypeGroupId);
                //淘宝分佣规则
                configPlan_wxwork.setTbCommissionPlanId(commission_id);
                //京东分佣规则
                configPlan_wxwork.setJdCommissionPlanId(commission_id);
                //拼多多分佣规则
                configPlan_wxwork.setPddCommissionPlanId(commission_id);
                //唯品会分佣规则
                configPlan_wxwork.setVphCommissionPlanId(commission_id);
                //抖音分佣规则
                configPlan_wxwork.setDyCommissionPlanId(commission_id);
                //本地生活分佣规则
                configPlan_wxwork.setLocalLiveCommissionPlanId(commission_id);

                //拉群规则
                if(configPlan_wxwork.getPullWxgroupRuleId() != null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getPullWxgroupRuleId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_wxwork.setPullWxgroupRuleId(id);
                    }
                }

                //微信好友添加插件id
                if(configPlan_wxwork.getWxUserAddId()!= null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getWxUserAddId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_wxwork.setWxUserAddId(id);
                    }
                }

                //推广裂变插件id
                if(configPlan_wxwork.getUserExpandFissionId()!= null ){
                    //查询推广裂变插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getUserExpandFissionId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加推广裂变插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的推广裂变插件id
                        configPlan_wxwork.setUserExpandFissionId(id);
                    }
                }

                //提现插件id
                if(configPlan_wxwork.getUserTakeBalId()!= null ){
                    //查询提现插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getUserTakeBalId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加提现插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);

                        //清空提现账房信息-每个运营商账房信息不一样
                        String data = sysPlugin.getData();
                        if(StringUtils.isNotBlank(data)){
                            try {
                                JSONObject jsonObject = JSONObject.parseObject(data);
                                if(jsonObject != null){
                                    jsonObject.put("zf_app_id", "");
                                    jsonObject.put("zf_app_key", "");
                                    jsonObject.put("wxpay_zf_app_id", "");
                                    jsonObject.put("wxpay_zf_app_key", "");
                                }
                                sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                            }catch (Exception e){
                                logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                            }
                        }

                        //修改配置方案中提现插件id
                        configPlan_wxwork.setUserTakeBalId(id);

                        //保存提现指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_TAKE_BAL.getValue(), sysPlugin.getData());
                    }
                }
                //好友引导插件id
                if(configPlan_wxwork.getUserGuideId()!= null ){
                    //好友引导插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getUserGuideId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加好友引导插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中好友引导插件id
                        configPlan_wxwork.setUserGuideId(id);
                    }
                }
                //自动备注插件id
                if(configPlan_wxwork.getUserAutoRemarkId()!= null ){
                    //自动备注插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getUserAutoRemarkId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加自动备注插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中自动备注插件id
                        configPlan_wxwork.setUserAutoRemarkId(id);
                    }
                }

                //订阅消息插件id
                if(configPlan_wxwork.getSubscribeMsgId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getSubscribeMsgId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加订阅消息插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中订阅消息插件id
                        configPlan_wxwork.setSubscribeMsgId(id);

                        //保存订阅指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.SUBSCRIBE_MSG.getValue(), sysPlugin.getData());
                    }
                }

                //个人信息查询插件id
                if(configPlan_wxwork.getUserInfoQueryId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getUserInfoQueryId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加个人信息查询插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中个人信息查询插件id
                        configPlan_wxwork.setUserInfoQueryId(id);

                        //保存个人信息指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_INFO_QUERY.getValue(), sysPlugin.getData());
                    }
                }

                //用户签到插件id
                if(configPlan_wxwork.getUserSigninId()!= null ){
                    //用户签到插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getUserSigninId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加用户签到插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中用户签到插件id
                        configPlan_wxwork.setUserSigninId(id);

                        //保存用户签到指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_SIGNIN.getValue(), sysPlugin.getData());

                    }
                }
                //社群管理插件id
                if(configPlan_wxwork.getCommunityId()!= null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getCommunityId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_wxwork.setCommunityId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //其他设置插件id
                if(configPlan_wxwork.getOtherId()!= null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getOtherId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wxwork.setOtherId(id);
                    }
                }

                //导购插件id
                if(configPlan_wxwork.getShoppingGuideId() != null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getShoppingGuideId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_wxwork.setShoppingGuideId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //微信公众号设置id
                if(configPlan_wxwork.getWxpubSettingId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getWxpubSettingId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wxwork.setWxpubSettingId(id);
                    }
                }

                //微信机器人关联公众号插件id
                if(configPlan_wxwork.getWechatUnionWxpubId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxwork.getWechatUnionWxpubId(), configPlan_wxwork.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wxwork.setWechatUnionWxpubId(id);
                    }
                }

                //批量插入插件
                if(sysPluginsList != null & sysPluginsList.size() > 0){
                    sysPluginMapper.batchAdd(sysPluginsList);
                    for(SysPlugin sysPlugin : sysPluginsList){
                        //修改插件更新缓存，确保缓存和数据库一致
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                    }
                }

                //查出旧指令列表
                List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_wxwork.getMerId(), configPlan_wxwork.getId());
                if(robotCmdList != null  && robotCmdList.size() > 0){
                    for (RobotCmd robotCmd : robotCmdList){
                        robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                    }

                    //批量插入指令表数据库
                    robotCmdMapper.batchAdd(robotCmdList);

                    //更新新的指令缓存redis
                    String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                }

                //添加新的配置方案
                configPlan_wxwork.setId(configPlanId);
                configPlan_wxwork.setMerId(merId);
                configPlan_wxwork.setCreateTime(now);
                configPlan_wxwork.setUpdateTime(now);
                configPlan_wxwork.setIsDefault(1);

                //清除联盟设置
                configPlan_wxwork.setTbAccountId(null);
                configPlan_wxwork.setJdAccountId(null);
                configPlan_wxwork.setPddAccountId(null);
                configPlan_wxwork.setVphAccountId(null);
                configPlan_wxwork.setTlbPddAccountId(null);
                configPlan_wxwork.setTlbMtAccountId(null);
                configPlan_wxwork.setElmAccountId(null);
                configPlan_wxwork.setTlbElmAccountId(null);
                configPlan_wxwork.setDyAccountId(null);

                configPlanMapper.insert(configPlan_wxwork);

            }
        }


        //微信公众号配置方案
        if(true){
            //查找默认的微信公众号配置方案
            ConfigPlan configPlan_wxpub = configPlanMapper.selectByIsDefault(defaultMerId, DeviceTypeEnum.WX_PUB.getKey());
            if(configPlan_wxpub != null){
                //复制插件件信息
                List<SysPlugin> sysPluginsList = new ArrayList<SysPlugin>();
                Long configPlanId = IdWorker.getId();

                //淘宝插件ID
                if(configPlan_wxpub.getTbId() != null ){
                    //查询淘宝插件
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getTbId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的淘宝插件
                        configPlan_wxpub.setTbId(id);
                    }
                }

                //京东插件id
                if(configPlan_wxpub.getJdId()!= null ){
                    //查询京东插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getJdId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加京东插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的京东插件id
                        configPlan_wxpub.setJdId(id);
                    }
                }

                //拼多多插件id
                if(configPlan_wxpub.getPddId()!= null ){
                    //查询拼多多插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getPddId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加拼多多插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的拼多多插件id
                        configPlan_wxpub.setPddId(id);
                    }
                }

                //唯品会插件id
                if(configPlan_wxpub.getVphId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getVphId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wxpub.setVphId(id);
                    }
                }

                //本地生活插件id
                if(configPlan_wxpub.getLocalLiveId()!= null ){
                    //查询本地生活插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getLocalLiveId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加本地生活插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的本地生活插件id
                        configPlan_wxpub.setLocalLiveId(id);

                        //本地生活指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.LOCAL_LIVE.getValue(), sysPlugin.getData());
                    }
                }

                //抖音插件id
                if(configPlan_wxpub.getDyId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getDyId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wxpub.setDyId(id);
                    }
                }

                //快手插件id
                if(configPlan_wxpub.getKsId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getKsId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_wxpub.setKsId(id);
                    }
                }

                //更新分佣规则
                //会员类型组
                configPlan_wxpub.setUserTypeGroupId(userTypeGroupId);
                //淘宝分佣规则
                configPlan_wxpub.setTbCommissionPlanId(commission_id);
                //京东分佣规则
                configPlan_wxpub.setJdCommissionPlanId(commission_id);
                //拼多多分佣规则
                configPlan_wxpub.setPddCommissionPlanId(commission_id);
                //唯品会分佣规则
                configPlan_wxpub.setVphCommissionPlanId(commission_id);
                //抖音分佣规则
                configPlan_wxpub.setDyCommissionPlanId(commission_id);
                //本地生活分佣规则
                configPlan_wxpub.setLocalLiveCommissionPlanId(commission_id);

                //拉群规则
                if(configPlan_wxpub.getPullWxgroupRuleId() != null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getPullWxgroupRuleId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_wxpub.setPullWxgroupRuleId(id);
                    }
                }

                //微信好友添加插件id
                if(configPlan_wxpub.getWxUserAddId()!= null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getWxUserAddId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_wxpub.setWxUserAddId(id);
                    }
                }

                //推广裂变插件id
                if(configPlan_wxpub.getUserExpandFissionId()!= null ){
                    //查询推广裂变插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getUserExpandFissionId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加推广裂变插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的推广裂变插件id
                        configPlan_wxpub.setUserExpandFissionId(id);
                    }
                }

                //提现插件id
                if(configPlan_wxpub.getUserTakeBalId()!= null ){
                    //查询提现插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getUserTakeBalId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加提现插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);

                        //清空提现账房信息-每个运营商账房信息不一样
                        String data = sysPlugin.getData();
                        if(StringUtils.isNotBlank(data)){
                            try {
                                JSONObject jsonObject = JSONObject.parseObject(data);
                                if(jsonObject != null){
                                    jsonObject.put("zf_app_id", "");
                                    jsonObject.put("zf_app_key", "");
                                    jsonObject.put("wxpay_zf_app_id", "");
                                    jsonObject.put("wxpay_zf_app_key", "");
                                }
                                sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                            }catch (Exception e){
                                logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                            }
                        }

                        //修改配置方案中提现插件id
                        configPlan_wxpub.setUserTakeBalId(id);

                        //保存提现指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_TAKE_BAL.getValue(), sysPlugin.getData());
                    }
                }

                //好友引导插件id
                if(configPlan_wxpub.getUserGuideId()!= null ){
                    //好友引导插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getUserGuideId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加好友引导插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中好友引导插件id
                        configPlan_wxpub.setUserGuideId(id);
                    }
                }
                //自动备注插件id
                if(configPlan_wxpub.getUserAutoRemarkId()!= null ){
                    //自动备注插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getUserAutoRemarkId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加自动备注插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中自动备注插件id
                        configPlan_wxpub.setUserAutoRemarkId(id);
                    }
                }

                //订阅消息插件id
                if(configPlan_wxpub.getSubscribeMsgId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getSubscribeMsgId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加订阅消息插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中订阅消息插件id
                        configPlan_wxpub.setSubscribeMsgId(id);

                        //保存订阅指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.SUBSCRIBE_MSG.getValue(), sysPlugin.getData());
                    }
                }

                //个人信息查询插件id
                if(configPlan_wxpub.getUserInfoQueryId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getUserInfoQueryId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加个人信息查询插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中个人信息查询插件id
                        configPlan_wxpub.setUserInfoQueryId(id);

                        //保存个人信息指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_INFO_QUERY.getValue(), sysPlugin.getData());
                    }
                }

                //用户签到插件id
                if(configPlan_wxpub.getUserSigninId()!= null ){
                    //用户签到插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getUserSigninId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加用户签到插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中用户签到插件id
                        configPlan_wxpub.setUserSigninId(id);

                        //保存用户签到指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_SIGNIN.getValue(), sysPlugin.getData());

                    }
                }
                //社群管理插件id
                if(configPlan_wxpub.getCommunityId()!= null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getCommunityId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_wxpub.setCommunityId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //其他设置插件id
                if(configPlan_wxpub.getOtherId()!= null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getOtherId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wxpub.setOtherId(id);
                    }
                }

                //导购插件id
                if(configPlan_wxpub.getShoppingGuideId() != null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getShoppingGuideId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_wxpub.setShoppingGuideId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //微信公众号设置id
                if(configPlan_wxpub.getWxpubSettingId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getWxpubSettingId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wxpub.setWxpubSettingId(id);
                    }
                }

                //微信机器人关联公众号插件id
                if(configPlan_wxpub.getWechatUnionWxpubId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wxpub.getWechatUnionWxpubId(), configPlan_wxpub.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_wxpub.setWechatUnionWxpubId(id);
                    }
                }

                //批量插入插件
                if(sysPluginsList != null & sysPluginsList.size() > 0){
                    sysPluginMapper.batchAdd(sysPluginsList);
                    for(SysPlugin sysPlugin : sysPluginsList){
                        //修改插件更新缓存，确保缓存和数据库一致
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                    }
                }

                //查出旧指令列表
                List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_wxpub.getMerId(), configPlan_wxpub.getId());
                if(robotCmdList != null && robotCmdList.size() > 0){
                    for (RobotCmd robotCmd : robotCmdList){
                        robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                    }

                    //批量插入指令表数据库
                    robotCmdMapper.batchAdd(robotCmdList);

                    //更新新的指令缓存redis
                    String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                }

                //添加新的配置方案
                configPlan_wxpub.setId(configPlanId);
                configPlan_wxpub.setMerId(merId);
                configPlan_wxpub.setCreateTime(now);
                configPlan_wxpub.setUpdateTime(now);
                configPlan_wxpub.setIsDefault(1);

                //清除联盟设置
                configPlan_wxpub.setTbAccountId(null);
                configPlan_wxpub.setJdAccountId(null);
                configPlan_wxpub.setPddAccountId(null);
                configPlan_wxpub.setVphAccountId(null);
                configPlan_wxpub.setTlbPddAccountId(null);
                configPlan_wxpub.setTlbMtAccountId(null);
                configPlan_wxpub.setElmAccountId(null);
                configPlan_wxpub.setTlbElmAccountId(null);
                configPlan_wxpub.setDyAccountId(null);

                configPlanMapper.insert(configPlan_wxpub);

            }
        }

        //个人微信群管理方案
        if(true){
            //查找默认的个人微信群管理方案
            ConfigPlan configPlan_private_group = configPlanMapper.selectByIsDefault(defaultMerId, DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey());
            if(configPlan_private_group != null){
                //复制插件件信息
                List<SysPlugin> sysPluginsList = new ArrayList<SysPlugin>();
                Long configPlanId = IdWorker.getId();

                //淘宝插件ID
                if(configPlan_private_group.getTbId() != null ){
                    //查询淘宝插件
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getTbId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的淘宝插件
                        configPlan_private_group.setTbId(id);
                    }
                }

                //京东插件id
                if(configPlan_private_group.getJdId()!= null ){
                    //查询京东插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getJdId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加京东插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的京东插件id
                        configPlan_private_group.setJdId(id);
                    }
                }

                //拼多多插件id
                if(configPlan_private_group.getPddId()!= null ){
                    //查询拼多多插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getPddId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加拼多多插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的拼多多插件id
                        configPlan_private_group.setPddId(id);
                    }
                }

                //唯品会插件id
                if(configPlan_private_group.getVphId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getVphId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_private_group.setVphId(id);
                    }
                }

                //本地生活插件id
                if(configPlan_private_group.getLocalLiveId()!= null ){
                    //查询本地生活插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getLocalLiveId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加本地生活插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的本地生活插件id
                        configPlan_private_group.setLocalLiveId(id);

                        //本地生活指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.LOCAL_LIVE.getValue(), sysPlugin.getData());
                    }
                }

                //抖音插件id
                if(configPlan_private_group.getDyId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getDyId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_private_group.setDyId(id);
                    }
                }

                //快手插件id
                if(configPlan_private_group.getKsId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getKsId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的快手插件id
                        configPlan_private_group.setKsId(id);
                    }
                }

                //更新分佣规则
                //会员类型组
                configPlan_private_group.setUserTypeGroupId(userTypeGroupId);
                //淘宝分佣规则
                configPlan_private_group.setTbCommissionPlanId(commission_id);
                //京东分佣规则
                configPlan_private_group.setJdCommissionPlanId(commission_id);
                //拼多多分佣规则
                configPlan_private_group.setPddCommissionPlanId(commission_id);
                //唯品会分佣规则
                configPlan_private_group.setVphCommissionPlanId(commission_id);
                //抖音分佣规则
                configPlan_private_group.setDyCommissionPlanId(commission_id);
                //本地生活分佣规则
                configPlan_private_group.setLocalLiveCommissionPlanId(commission_id);

                //拉群规则
                if(configPlan_private_group.getPullWxgroupRuleId() != null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getPullWxgroupRuleId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_private_group.setPullWxgroupRuleId(id);
                    }
                }

                //微信好友添加插件id
                if(configPlan_private_group.getWxUserAddId()!= null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getWxUserAddId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_private_group.setWxUserAddId(id);
                    }
                }

                //推广裂变插件id
                if(configPlan_private_group.getUserExpandFissionId()!= null ){
                    //查询推广裂变插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getUserExpandFissionId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加推广裂变插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的推广裂变插件id
                        configPlan_private_group.setUserExpandFissionId(id);
                    }
                }

                //提现插件id
                if(configPlan_private_group.getUserTakeBalId()!= null ){
                    //查询提现插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getUserTakeBalId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加提现插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);

                        //清空提现账房信息-每个运营商账房信息不一样
                        String data = sysPlugin.getData();
                        if(StringUtils.isNotBlank(data)){
                            try {
                                JSONObject jsonObject = JSONObject.parseObject(data);
                                if(jsonObject != null){
                                    jsonObject.put("zf_app_id", "");
                                    jsonObject.put("zf_app_key", "");
                                    jsonObject.put("wxpay_zf_app_id", "");
                                    jsonObject.put("wxpay_zf_app_key", "");
                                }
                                sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                            }catch (Exception e){
                                logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                            }
                        }

                        //修改配置方案中提现插件id
                        configPlan_private_group.setUserTakeBalId(id);

                        //保存提现指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_TAKE_BAL.getValue(), sysPlugin.getData());
                    }
                }
                //好友引导插件id
                if(configPlan_private_group.getUserGuideId()!= null ){
                    //好友引导插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getUserGuideId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加好友引导插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中好友引导插件id
                        configPlan_private_group.setUserGuideId(id);
                    }
                }
                //自动备注插件id
                if(configPlan_private_group.getUserAutoRemarkId()!= null ){
                    //自动备注插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getUserAutoRemarkId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加自动备注插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中自动备注插件id
                        configPlan_private_group.setUserAutoRemarkId(id);
                    }
                }

                //订阅消息插件id
                if(configPlan_private_group.getSubscribeMsgId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getSubscribeMsgId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加订阅消息插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中订阅消息插件id
                        configPlan_private_group.setSubscribeMsgId(id);

                        //保存订阅指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.SUBSCRIBE_MSG.getValue(), sysPlugin.getData());
                    }
                }

                //个人信息查询插件id
                if(configPlan_private_group.getUserInfoQueryId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getUserInfoQueryId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加个人信息查询插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中个人信息查询插件id
                        configPlan_private_group.setUserInfoQueryId(id);

                        //保存个人信息指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_INFO_QUERY.getValue(), sysPlugin.getData());
                    }
                }

                //用户签到插件id
                if(configPlan_private_group.getUserSigninId()!= null ){
                    //用户签到插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getUserSigninId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加用户签到插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中用户签到插件id
                        configPlan_private_group.setUserSigninId(id);

                        //保存用户签到指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_SIGNIN.getValue(), sysPlugin.getData());

                    }
                }
                //社群管理插件id
                if(configPlan_private_group.getCommunityId()!= null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getCommunityId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_private_group.setCommunityId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //其他设置插件id
                if(configPlan_private_group.getOtherId()!= null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getOtherId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_private_group.setOtherId(id);
                    }
                }

                //导购插件id
                if(configPlan_private_group.getShoppingGuideId() != null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getShoppingGuideId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_private_group.setShoppingGuideId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //微信公众号设置id
                if(configPlan_private_group.getWxpubSettingId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getWxpubSettingId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_private_group.setWxpubSettingId(id);
                    }
                }

                //微信机器人关联公众号插件id
                if(configPlan_private_group.getWechatUnionWxpubId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getWechatUnionWxpubId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_private_group.setWechatUnionWxpubId(id);
                    }
                }

                //群功能设置
                if(configPlan_private_group.getGroupBasesetId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_private_group.getGroupBasesetId(), configPlan_private_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_private_group.setGroupBasesetId(id);
                    }
                    //如果群管理功能设置配置方案，默认添加群管理功能设置插件
                    if(configPlan_private_group.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }else if(configPlan_private_group.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }else if(configPlan_private_group.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }
                }

                //批量插入插件
                if(sysPluginsList != null & sysPluginsList.size() > 0){
                    sysPluginMapper.batchAdd(sysPluginsList);
                    for(SysPlugin sysPlugin : sysPluginsList){
                        //修改插件更新缓存，确保缓存和数据库一致
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                    }
                }

                //查出旧指令列表
                List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_private_group.getMerId(), configPlan_private_group.getId());
                if(robotCmdList != null && robotCmdList.size() > 0){
                    for (RobotCmd robotCmd : robotCmdList){
                        robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                    }

                    //批量插入指令表数据库
                    robotCmdMapper.batchAdd(robotCmdList);

                    //更新新的指令缓存redis
                    String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                }

                //添加新的配置方案
                configPlan_private_group.setId(configPlanId);
                configPlan_private_group.setMerId(merId);
                configPlan_private_group.setCreateTime(now);
                configPlan_private_group.setUpdateTime(now);
                configPlan_private_group.setIsDefault(1);

                //清除联盟设置
                configPlan_private_group.setTbAccountId(null);
                configPlan_private_group.setJdAccountId(null);
                configPlan_private_group.setPddAccountId(null);
                configPlan_private_group.setVphAccountId(null);
                configPlan_private_group.setTlbPddAccountId(null);
                configPlan_private_group.setTlbMtAccountId(null);
                configPlan_private_group.setElmAccountId(null);
                configPlan_private_group.setTlbElmAccountId(null);
                configPlan_private_group.setDyAccountId(null);

                configPlanMapper.insert(configPlan_private_group);

            }
        }


        //企业微信群管理方案
        if(true){
            //查找默认的企业微信群管理方案
            ConfigPlan configPlan_work_group = configPlanMapper.selectByIsDefault(defaultMerId, DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey());
            if(configPlan_work_group != null){
                //复制插件件信息
                List<SysPlugin> sysPluginsList = new ArrayList<SysPlugin>();
                Long configPlanId = IdWorker.getId();

                //淘宝插件ID
                if(configPlan_work_group.getTbId() != null ){
                    //查询淘宝插件
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getTbId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的淘宝插件
                        configPlan_work_group.setTbId(id);
                    }
                }

                //京东插件id
                if(configPlan_work_group.getJdId()!= null ){
                    //查询京东插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getJdId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加京东插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的京东插件id
                        configPlan_work_group.setJdId(id);
                    }
                }

                //拼多多插件id
                if(configPlan_work_group.getPddId()!= null ){
                    //查询拼多多插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getPddId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加拼多多插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的拼多多插件id
                        configPlan_work_group.setPddId(id);
                    }
                }

                //唯品会插件id
                if(configPlan_work_group.getVphId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getVphId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_work_group.setVphId(id);
                    }
                }

                //本地生活插件id
                if(configPlan_work_group.getLocalLiveId()!= null ){
                    //查询本地生活插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getLocalLiveId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加本地生活插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的本地生活插件id
                        configPlan_work_group.setLocalLiveId(id);

                    }
                }

                //抖音插件id
                if(configPlan_work_group.getDyId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getDyId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_work_group.setDyId(id);
                    }
                }

                //快手插件id
                if(configPlan_work_group.getKsId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getKsId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的快手插件id
                        configPlan_work_group.setKsId(id);
                    }
                }

                //更新分佣规则
                //会员类型组
                configPlan_work_group.setUserTypeGroupId(userTypeGroupId);
                //淘宝分佣规则
                configPlan_work_group.setTbCommissionPlanId(commission_id);
                //京东分佣规则
                configPlan_work_group.setJdCommissionPlanId(commission_id);
                //拼多多分佣规则
                configPlan_work_group.setPddCommissionPlanId(commission_id);
                //唯品会分佣规则
                configPlan_work_group.setVphCommissionPlanId(commission_id);
                //抖音分佣规则
                configPlan_work_group.setDyCommissionPlanId(commission_id);
                //本地生活分佣规则
                configPlan_work_group.setLocalLiveCommissionPlanId(commission_id);

                //拉群规则
                if(configPlan_work_group.getPullWxgroupRuleId() != null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getPullWxgroupRuleId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_work_group.setPullWxgroupRuleId(id);
                    }
                }

                //微信好友添加插件id
                if(configPlan_work_group.getWxUserAddId()!= null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getWxUserAddId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_work_group.setWxUserAddId(id);
                    }
                }

                //推广裂变插件id
                if(configPlan_work_group.getUserExpandFissionId()!= null ){
                    //查询推广裂变插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getUserExpandFissionId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加推广裂变插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的推广裂变插件id
                        configPlan_work_group.setUserExpandFissionId(id);
                    }
                }

                //提现插件id
                if(configPlan_work_group.getUserTakeBalId()!= null ){
                    //查询提现插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getUserTakeBalId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加提现插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);

                        //清空提现账房信息-每个运营商账房信息不一样
                        String data = sysPlugin.getData();
                        if(StringUtils.isNotBlank(data)){
                            try {
                                JSONObject jsonObject = JSONObject.parseObject(data);
                                if(jsonObject != null){
                                    jsonObject.put("zf_app_id", "");
                                    jsonObject.put("zf_app_key", "");
                                    jsonObject.put("wxpay_zf_app_id", "");
                                    jsonObject.put("wxpay_zf_app_key", "");
                                }
                                sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                            }catch (Exception e){
                                logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                            }
                        }

                        //修改配置方案中提现插件id
                        configPlan_work_group.setUserTakeBalId(id);

                    }
                }

                //好友引导插件id
                if(configPlan_work_group.getUserGuideId()!= null ){
                    //好友引导插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getUserGuideId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加好友引导插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中好友引导插件id
                        configPlan_work_group.setUserGuideId(id);
                    }
                }

                //自动备注插件id
                if(configPlan_work_group.getUserAutoRemarkId()!= null ){
                    //自动备注插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getUserAutoRemarkId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加自动备注插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中自动备注插件id
                        configPlan_work_group.setUserAutoRemarkId(id);
                    }
                }

                //订阅消息插件id
                if(configPlan_work_group.getSubscribeMsgId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getSubscribeMsgId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加订阅消息插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中订阅消息插件id
                        configPlan_work_group.setSubscribeMsgId(id);

                    }
                }

                //个人信息查询插件id
                if(configPlan_work_group.getUserInfoQueryId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getUserInfoQueryId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加个人信息查询插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中个人信息查询插件id
                        configPlan_work_group.setUserInfoQueryId(id);

                    }
                }

                //用户签到插件id
                if(configPlan_work_group.getUserSigninId()!= null ){
                    //用户签到插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getUserSigninId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加用户签到插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中用户签到插件id
                        configPlan_work_group.setUserSigninId(id);

                    }
                }
                //社群管理插件id
                if(configPlan_work_group.getCommunityId()!= null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getCommunityId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_work_group.setCommunityId(id);

                    }
                }

                //其他设置插件id
                if(configPlan_work_group.getOtherId()!= null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getOtherId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_work_group.setOtherId(id);
                    }
                }

                //导购插件id
                if(configPlan_work_group.getShoppingGuideId() != null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getShoppingGuideId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_work_group.setShoppingGuideId(id);

                    }
                }

                //微信公众号设置id
                if(configPlan_work_group.getWxpubSettingId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getWxpubSettingId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_work_group.setWxpubSettingId(id);
                    }
                }

                //微信机器人关联公众号插件id
                if(configPlan_work_group.getWechatUnionWxpubId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getWechatUnionWxpubId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_work_group.setWechatUnionWxpubId(id);
                    }
                }

                //群功能设置
                if(configPlan_work_group.getGroupBasesetId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_work_group.getGroupBasesetId(), configPlan_work_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_work_group.setGroupBasesetId(id);
                    }

                    //如果群管理功能设置配置方案，默认添加群管理功能设置插件
                    if(configPlan_work_group.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }else if(configPlan_work_group.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }else if(configPlan_work_group.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }
                }



                //批量插入插件
                if(sysPluginsList != null & sysPluginsList.size() > 0){
                    sysPluginMapper.batchAdd(sysPluginsList);
                    for(SysPlugin sysPlugin : sysPluginsList){
                        //修改插件更新缓存，确保缓存和数据库一致
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                    }
                }

                //查出旧指令列表
                List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_work_group.getMerId(), configPlan_work_group.getId());
                if(robotCmdList != null && robotCmdList.size() > 0){
                    for (RobotCmd robotCmd : robotCmdList){
                        robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                    }

                    //批量插入指令表数据库
                    robotCmdMapper.batchAdd(robotCmdList);

                    //更新新的指令缓存redis
                    String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                }

                //添加新的配置方案
                configPlan_work_group.setId(configPlanId);
                configPlan_work_group.setMerId(merId);
                configPlan_work_group.setCreateTime(now);
                configPlan_work_group.setUpdateTime(now);
                configPlan_work_group.setIsDefault(1);

                //清除联盟设置
                configPlan_work_group.setTbAccountId(null);
                configPlan_work_group.setJdAccountId(null);
                configPlan_work_group.setPddAccountId(null);
                configPlan_work_group.setVphAccountId(null);
                configPlan_work_group.setTlbPddAccountId(null);
                configPlan_work_group.setTlbMtAccountId(null);
                configPlan_work_group.setElmAccountId(null);
                configPlan_work_group.setTlbElmAccountId(null);
                configPlan_work_group.setDyAccountId(null);

                configPlanMapper.insert(configPlan_work_group);

            }
        }

        //抖音微信群管理方案
        if(true){
            //查找默认的企业微信群管理方案
            ConfigPlan configPlan_tiktok_group = configPlanMapper.selectByIsDefault(defaultMerId, DeviceTypeGroupEnum.TIKTOK_GROUP.getKey());
            if(configPlan_tiktok_group != null){
                //复制插件件信息
                List<SysPlugin> sysPluginsList = new ArrayList<SysPlugin>();
                Long configPlanId = IdWorker.getId();

                //淘宝插件ID
                if(configPlan_tiktok_group.getTbId() != null ){
                    //查询淘宝插件
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getTbId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的淘宝插件
                        configPlan_tiktok_group.setTbId(id);
                    }
                }

                //京东插件id
                if(configPlan_tiktok_group.getJdId()!= null ){
                    //查询京东插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getJdId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加京东插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的京东插件id
                        configPlan_tiktok_group.setJdId(id);
                    }
                }

                //拼多多插件id
                if(configPlan_tiktok_group.getPddId()!= null ){
                    //查询拼多多插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getPddId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加拼多多插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的拼多多插件id
                        configPlan_tiktok_group.setPddId(id);
                    }
                }

                //唯品会插件id
                if(configPlan_tiktok_group.getVphId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getVphId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_tiktok_group.setVphId(id);
                    }
                }

                //本地生活插件id
                if(configPlan_tiktok_group.getLocalLiveId()!= null ){
                    //查询本地生活插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getLocalLiveId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加本地生活插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的本地生活插件id
                        configPlan_tiktok_group.setLocalLiveId(id);

                    }
                }

                //抖音插件id
                if(configPlan_tiktok_group.getDyId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getDyId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_tiktok_group.setDyId(id);
                    }
                }

                //快手插件id
                if(configPlan_tiktok_group.getKsId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getKsId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的快手插件id
                        configPlan_tiktok_group.setKsId(id);
                    }
                }

                //更新分佣规则
                //会员类型组
                configPlan_tiktok_group.setUserTypeGroupId(userTypeGroupId);
                //淘宝分佣规则
                configPlan_tiktok_group.setTbCommissionPlanId(commission_id);
                //京东分佣规则
                configPlan_tiktok_group.setJdCommissionPlanId(commission_id);
                //拼多多分佣规则
                configPlan_tiktok_group.setPddCommissionPlanId(commission_id);
                //唯品会分佣规则
                configPlan_tiktok_group.setVphCommissionPlanId(commission_id);
                //抖音分佣规则
                configPlan_tiktok_group.setDyCommissionPlanId(commission_id);
                //本地生活分佣规则
                configPlan_tiktok_group.setLocalLiveCommissionPlanId(commission_id);

                //拉群规则
                if(configPlan_tiktok_group.getPullWxgroupRuleId() != null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getPullWxgroupRuleId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_tiktok_group.setPullWxgroupRuleId(id);
                    }
                }

                //微信好友添加插件id
                if(configPlan_tiktok_group.getWxUserAddId()!= null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getWxUserAddId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_tiktok_group.setWxUserAddId(id);
                    }
                }

                //推广裂变插件id
                if(configPlan_tiktok_group.getUserExpandFissionId()!= null ){
                    //查询推广裂变插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getUserExpandFissionId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加推广裂变插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的推广裂变插件id
                        configPlan_tiktok_group.setUserExpandFissionId(id);
                    }
                }

                //提现插件id
                if(configPlan_tiktok_group.getUserTakeBalId()!= null ){
                    //查询提现插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getUserTakeBalId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加提现插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);

                        //清空提现账房信息-每个运营商账房信息不一样
                        String data = sysPlugin.getData();
                        if(StringUtils.isNotBlank(data)){
                            try {
                                JSONObject jsonObject = JSONObject.parseObject(data);
                                if(jsonObject != null){
                                    jsonObject.put("zf_app_id", "");
                                    jsonObject.put("zf_app_key", "");
                                    jsonObject.put("wxpay_zf_app_id", "");
                                    jsonObject.put("wxpay_zf_app_key", "");
                                }
                                sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                            }catch (Exception e){
                                logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                            }
                        }

                        //修改配置方案中提现插件id
                        configPlan_tiktok_group.setUserTakeBalId(id);

                    }
                }

                //好友引导插件id
                if(configPlan_tiktok_group.getUserGuideId()!= null ){
                    //好友引导插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getUserGuideId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加好友引导插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中好友引导插件id
                        configPlan_tiktok_group.setUserGuideId(id);
                    }
                }

                //自动备注插件id
                if(configPlan_tiktok_group.getUserAutoRemarkId()!= null ){
                    //自动备注插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getUserAutoRemarkId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加自动备注插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中自动备注插件id
                        configPlan_tiktok_group.setUserAutoRemarkId(id);
                    }
                }

                //订阅消息插件id
                if(configPlan_tiktok_group.getSubscribeMsgId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getSubscribeMsgId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加订阅消息插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中订阅消息插件id
                        configPlan_tiktok_group.setSubscribeMsgId(id);

                    }
                }

                //个人信息查询插件id
                if(configPlan_tiktok_group.getUserInfoQueryId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getUserInfoQueryId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加个人信息查询插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中个人信息查询插件id
                        configPlan_tiktok_group.setUserInfoQueryId(id);

                    }
                }

                //用户签到插件id
                if(configPlan_tiktok_group.getUserSigninId()!= null ){
                    //用户签到插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getUserSigninId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加用户签到插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中用户签到插件id
                        configPlan_tiktok_group.setUserSigninId(id);

                    }
                }
                //社群管理插件id
                if(configPlan_tiktok_group.getCommunityId()!= null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getCommunityId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_tiktok_group.setCommunityId(id);

                    }
                }

                //其他设置插件id
                if(configPlan_tiktok_group.getOtherId()!= null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getOtherId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_tiktok_group.setOtherId(id);
                    }
                }

                //导购插件id
                if(configPlan_tiktok_group.getShoppingGuideId() != null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getShoppingGuideId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_tiktok_group.setShoppingGuideId(id);

                    }
                }

                //微信公众号设置id
                if(configPlan_tiktok_group.getWxpubSettingId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getWxpubSettingId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_tiktok_group.setWxpubSettingId(id);
                    }
                }

                //微信机器人关联公众号插件id
                if(configPlan_tiktok_group.getWechatUnionWxpubId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getWechatUnionWxpubId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_tiktok_group.setWechatUnionWxpubId(id);
                    }
                }

                //群功能设置
                if(configPlan_tiktok_group.getGroupBasesetId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_tiktok_group.getGroupBasesetId(), configPlan_tiktok_group.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_tiktok_group.setGroupBasesetId(id);
                    }

                    //如果群管理功能设置配置方案，默认添加群管理功能设置插件
                    if(configPlan_tiktok_group.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }else if(configPlan_tiktok_group.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }else if(configPlan_tiktok_group.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                        String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                        redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                    }
                }

                //批量插入插件
                if(sysPluginsList != null & sysPluginsList.size() > 0){
                    sysPluginMapper.batchAdd(sysPluginsList);
                    for(SysPlugin sysPlugin : sysPluginsList){
                        //修改插件更新缓存，确保缓存和数据库一致
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                    }
                }

                //查出旧指令列表
                List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_tiktok_group.getMerId(), configPlan_tiktok_group.getId());
                if(robotCmdList != null && robotCmdList.size() > 0){
                    for (RobotCmd robotCmd : robotCmdList){
                        robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                    }

                    //批量插入指令表数据库
                    robotCmdMapper.batchAdd(robotCmdList);

                    //更新新的指令缓存redis
                    String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                }

                //添加新的配置方案
                configPlan_tiktok_group.setId(configPlanId);
                configPlan_tiktok_group.setMerId(merId);
                configPlan_tiktok_group.setCreateTime(now);
                configPlan_tiktok_group.setUpdateTime(now);
                configPlan_tiktok_group.setIsDefault(1);

                //清除联盟设置
                configPlan_tiktok_group.setTbAccountId(null);
                configPlan_tiktok_group.setJdAccountId(null);
                configPlan_tiktok_group.setPddAccountId(null);
                configPlan_tiktok_group.setVphAccountId(null);
                configPlan_tiktok_group.setTlbPddAccountId(null);
                configPlan_tiktok_group.setTlbMtAccountId(null);
                configPlan_tiktok_group.setElmAccountId(null);
                configPlan_tiktok_group.setTlbElmAccountId(null);
                configPlan_tiktok_group.setDyAccountId(null);

                configPlanMapper.insert(configPlan_tiktok_group);

            }
        }


        //app配置方案
        if(true){
            //查找APP的配置方案
            ConfigPlan configPlan_app = configPlanMapper.selectAppConfigPlan(defaultMerId);
            if(configPlan_app != null){
                //复制插件件信息
                List<SysPlugin> sysPluginsList = new ArrayList<SysPlugin>();
                Long configPlanId = IdWorker.getId();

                //淘宝插件ID
                if(configPlan_app.getTbId() != null ){
                    //查询淘宝插件
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getTbId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的淘宝插件
                        configPlan_app.setTbId(id);
                    }
                }

                //京东插件id
                if(configPlan_app.getJdId()!= null ){
                    //查询京东插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getJdId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加京东插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的京东插件id
                        configPlan_app.setJdId(id);
                    }
                }

                //拼多多插件id
                if(configPlan_app.getPddId()!= null ){
                    //查询拼多多插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getPddId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加拼多多插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的拼多多插件id
                        configPlan_app.setPddId(id);
                    }
                }

                //唯品会插件id
                if(configPlan_app.getVphId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getVphId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_app.setVphId(id);
                    }
                }

                //本地生活插件id
                if(configPlan_app.getLocalLiveId()!= null ){
                    //查询本地生活插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getLocalLiveId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加本地生活插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的本地生活插件id
                        configPlan_app.setLocalLiveId(id);

                        //本地生活指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.LOCAL_LIVE.getValue(), sysPlugin.getData());
                    }
                }

                //抖音插件id
                if(configPlan_app.getDyId()!= null ){
                    //查询唯品会插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getDyId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加唯品会插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的唯品会插件id
                        configPlan_app.setDyId(id);
                    }
                }

                //更新分佣规则
                //会员类型组
                configPlan_app.setUserTypeGroupId(userTypeGroupId);
                //淘宝分佣规则
                configPlan_app.setTbCommissionPlanId(commission_id);
                //京东分佣规则
                configPlan_app.setJdCommissionPlanId(commission_id);
                //拼多多分佣规则
                configPlan_app.setPddCommissionPlanId(commission_id);
                //唯品会分佣规则
                configPlan_app.setVphCommissionPlanId(commission_id);
                //抖音分佣规则
                configPlan_app.setDyCommissionPlanId(commission_id);
                //本地生活分佣规则
                configPlan_app.setLocalLiveCommissionPlanId(commission_id);


                //微信好友添加插件id
                if(configPlan_app.getWxUserAddId()!= null ){
                    //查询微信好友添加插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getWxUserAddId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加微信好友添加插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案的微信好友添加插件id
                        configPlan_app.setWxUserAddId(id);
                    }
                }

                //推广裂变插件id
                if(configPlan_app.getUserExpandFissionId()!= null ){
                    //查询推广裂变插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getUserExpandFissionId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加推广裂变插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);

                        //清空提现账房信息-每个运营商账房信息不一样
                        String data = sysPlugin.getData();
                        if(StringUtils.isNotBlank(data)){
                            try {
                                JSONObject jsonObject = JSONObject.parseObject(data);
                                if(jsonObject != null){
                                    jsonObject.put("zf_app_id", "");
                                    jsonObject.put("zf_app_key", "");
                                    jsonObject.put("wxpay_zf_app_id", "");
                                    jsonObject.put("wxpay_zf_app_key", "");
                                }
                                sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                            }catch (Exception e){
                                logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                            }
                        }

                        //修改配置方案的推广裂变插件id
                        configPlan_app.setUserExpandFissionId(id);
                    }
                }

                //提现插件id
                if(configPlan_app.getUserTakeBalId()!= null ){
                    //查询提现插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getUserTakeBalId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加提现插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中提现插件id
                        configPlan_app.setUserTakeBalId(id);

                        //保存提现指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_TAKE_BAL.getValue(), sysPlugin.getData());
                    }
                }

                //好友引导插件id
                if(configPlan_app.getUserGuideId()!= null ){
                    //好友引导插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getUserGuideId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加好友引导插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中好友引导插件id
                        configPlan_app.setUserGuideId(id);
                    }
                }

                //自动备注插件id
                if(configPlan_app.getUserAutoRemarkId()!= null ){
                    //自动备注插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getUserAutoRemarkId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加自动备注插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中自动备注插件id
                        configPlan_app.setUserAutoRemarkId(id);
                    }
                }

                //订阅消息插件id
                if(configPlan_app.getSubscribeMsgId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getSubscribeMsgId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加订阅消息插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中订阅消息插件id
                        configPlan_app.setSubscribeMsgId(id);

                        //保存订阅指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.SUBSCRIBE_MSG.getValue(), sysPlugin.getData());
                    }
                }

                //个人信息查询插件id
                if(configPlan_app.getUserInfoQueryId()!= null ){
                    //订阅消息插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getUserInfoQueryId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加个人信息查询插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中个人信息查询插件id
                        configPlan_app.setUserInfoQueryId(id);

                        //保存个人信息指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_INFO_QUERY.getValue(), sysPlugin.getData());
                    }
                }

                //用户签到插件id
                if(configPlan_app.getUserSigninId()!= null ){
                    //用户签到插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getUserSigninId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加用户签到插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中用户签到插件id
                        configPlan_app.setUserSigninId(id);

                        //保存用户签到指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_SIGNIN.getValue(), sysPlugin.getData());
                    }
                }

                //社群管理插件id
                if(configPlan_app.getCommunityId()!= null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getCommunityId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_app.setCommunityId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //其他设置插件id
                if(configPlan_app.getOtherId()!= null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getOtherId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_app.setOtherId(id);
                    }
                }

                //导购插件id
                if(configPlan_app.getShoppingGuideId() != null ){
                    //社群管理插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getShoppingGuideId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加社群管理插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中社群管理插件id
                        configPlan_app.setShoppingGuideId(id);

                        //保存社群指令
                        //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
                    }
                }

                //微信公众号设置id
                if(configPlan_app.getWxpubSettingId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getWxpubSettingId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_app.setWxpubSettingId(id);
                    }
                }

                //微信机器人关联公众号插件id
                if(configPlan_app.getWechatUnionWxpubId() != null ){
                    //其他设置插件id
                    SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_app.getWechatUnionWxpubId(), configPlan_app.getMerId());
                    if(sysPlugin != null){
                        Long id = IdWorker.getId();
                        sysPlugin.setId(id);
                        sysPlugin.setMerId(merId);
                        sysPlugin.setCreateTime(now);
                        sysPlugin.setUpdateTime(now);
                        //新加其他设置插件id
                        //sysPluginMapper.insert(sysPlugin);
                        sysPluginsList.add(sysPlugin);
                        //修改配置方案中其他设置插件id
                        configPlan_app.setWechatUnionWxpubId(id);
                    }
                }

                //批量插入插件
                if(sysPluginsList != null & sysPluginsList.size() > 0){
                    sysPluginMapper.batchAdd(sysPluginsList);
                    for(SysPlugin sysPlugin : sysPluginsList){
                        //修改插件更新缓存，确保缓存和数据库一致
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                    }
                }

                //查出旧指令列表
                List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_app.getMerId(), configPlan_app.getId());
                if(robotCmdList != null  && robotCmdList.size() > 0){
                    for (RobotCmd robotCmd : robotCmdList){
                        robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                    }

                    //批量插入指令表数据库
                    robotCmdMapper.batchAdd(robotCmdList);

                    //更新新的指令缓存redis
                    String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                }

                //添加新的配置方案
                configPlan_app.setId(configPlanId);
                configPlan_app.setMerId(merId);
                configPlan_app.setCreateTime(now);
                configPlan_app.setUpdateTime(now);
                configPlan_app.setIsDefault(1);

                //清除联盟设置
                configPlan_app.setTbAccountId(null);
                configPlan_app.setJdAccountId(null);
                configPlan_app.setPddAccountId(null);
                configPlan_app.setVphAccountId(null);
                configPlan_app.setTlbPddAccountId(null);
                configPlan_app.setTlbMtAccountId(null);
                configPlan_app.setElmAccountId(null);
                configPlan_app.setTlbElmAccountId(null);
                configPlan_app.setDyAccountId(null);

                configPlanMapper.insert(configPlan_app);

            }
        }


    }



}
